<template>
  <div class="task-list-container">
    <CPageTable @register="registerPage" v-model:search-form="searchForm">
      <template #headerRightButton>
        <el-button type="primary" @click="handleAdd" v-btnPermission="'TAA_ADD_REDUCTION_PROJECT'"
          ><CIcon type="c-xian-xinjian" class="g-mr4"></CIcon>新增</el-button
        >
      </template>
      <template #factoryName="{ scope }">
        <!-- {{ empty(scope.row.projectNo) }} -->
        <CTableColumn
          :main-text="scope.row.factoryName"
          :sub-text="scope.row.projectNo"
        ></CTableColumn>
      </template>
      <template #projectTime="{ scope }">
        <template v-if="scope.row.projectTime">
          <p>{{ datjs(scope.row.projectTime).format('YYYY-MM-DD') }}</p>
          <p>{{ datjs(scope.row.projectTime).format('HH:mm:ss') }}</p>
        </template>
        <span v-else>-</span>
      </template>
      <template #creator="{ scope }">
        <CTableColumn
          :main-text="scope.row.creator"
          :sub-text="scope.row.createTime"
        ></CTableColumn>
      </template>
      <template #reduction="{ scope }">
        <p class="sub-text">CO₂：{{ scope.row.coReduction || '-' }}</p>
        <p class="sub-text">SO2：{{ scope.row.soReduction || '-' }}</p>
        <p class="sub-text">NO2：{{ scope.row.noReduction || '-' }}</p>
        <p class="sub-text">VOCS：{{ scope.row.vocsReduction || '-' }}</p>
      </template>
      <template #file="{ scope }">
        <CTableColumn
          v-for="(item, index) in scope.row.file"
          :key="index"
          :main-text="item.fileName"
          subTextHideIfEmpty
          isLink
          @click="handleLinkTo(item.fileUrl)"
        ></CTableColumn>
      </template>
      <template #op="{ scope }">
        <CTableButtonGroup :buttons-config="getTableButtonsConfig(scope.row)" />
      </template>
    </CPageTable>
    <ModifyDialog
      v-model:visible="showDialog"
      :rowData="dialogRowData"
      :type="dialogType"
      @refresh="refresh"
    ></ModifyDialog>
  </div>
</template>
<script lang="ts">
export default {
  name: 'CarbonReductionProject'
};
</script>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { GetReductionListResult } from '@/apis/taa/target/model/reductionModel';
import ModifyDialog from './components/modifyDialog/index.vue';
import { CPageTable, useCPageTable, CPageTableProps } from '@/components/CPageTable';
import { CTableColumn, CTableButtonGroup } from '@/components/CTable';
import type { TableButtonItem } from '@/components/CTable';
import { useTableList } from './useTableList';
import { useMessage } from '@/hooks/useMessage';
import { useKeepAliveRefresh } from '@/hooks/useKeepAliveRefresh';
import { empty } from '@/utils';
import datjs from 'dayjs';
import { delReduction } from '@/apis/taa/target/reduction';

const { createMessage, createConfirm } = useMessage();
const { searchForm, searchOptions, tableOptions, tableFieldsSetting } = useTableList();
const options = reactive<CPageTableProps>({
  title: '工厂碳减排项目管理',
  searchOptions: searchOptions,
  tableOptions,
  tableFieldsSetting,
  enableFieldsSetting: true
});
const [registerPage, { refresh }] = useCPageTable(options);
useKeepAliveRefresh(refresh);

/**
 * 配置表格的按钮
 * @param row 表格行数据
 */
function getTableButtonsConfig(row: GetReductionListResult): TableButtonItem[] {
  return [
    {
      text: '修改',
      btnPermission: 'TAA_EDIT_REDUCTION_PROJECT',
      onClick: () => handleEdit(row)
    },
    {
      text: '删除',
      btnPermission: 'TAA_DELETE_REDUCTION_PROJECT',
      onClick: () => handleDelete(row)
    }
  ];
}

const showDialog = ref(false);
const dialogRowData = ref({});
const dialogType = ref<'ADD' | 'EDIT'>('ADD');

const handleDelete = (row: GetReductionListResult) => {
  createConfirm('提示', '确认删除该碳减排项目？', {
    iconType: 'warning',
    confirmButtonText: '删除'
  }).then(() => {
    delReduction(row.projectNo).then(() => {
      createMessage.success('删除成功！');
      refresh();
    });
  });
};
const handleEdit = (row: GetReductionListResult) => {
  dialogRowData.value = row;
  dialogType.value = 'EDIT';
  showDialog.value = true;
};
const handleAdd = () => {
  console.log('打开新增弹框');
  dialogRowData.value = {};
  dialogType.value = 'ADD';
  showDialog.value = true;
};

const handleLinkTo = (fileUrl: string) => {
  console.log(fileUrl, 'fileUrl');
  if (!fileUrl) {
    return false;
  }
  window.open(fileUrl, '_blank');
};
</script>

<style lang="scss" scoped>
.sub-text {
  font-size: 12px;
  font-weight: 400;
  line-height: 20px;
}
</style>
